<template>
  <div class="container" style="padding: 20px 40px 0 40px;">
    <div class="app-container">
      <el-row>
        <el-col :span="24">楼宇名称：
          <el-input v-model="pageParams.name" placeholder="请输入楼宇名称" style="width: 200px;" />
          <el-button type="primary" style="border-radius: 5px; margin-left: 20px; font-size: 17px;line-height: 12px;" @click="queryBuild">查询</el-button>
        </el-col>
      </el-row>
      <el-row style="margin-top: 10px;">
        <el-col :span="24">
          <el-button type="primary" style="font-size: 17px;border-radius: 5px;margin: 20px 0;" @click="showDialog=true">添加楼宇</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="buildingList" style="width: 100%">
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="180"
        />
        <el-table-column
          align="center"
          prop="name"
          label="楼宇名称"
          width="180"
        />
        <el-table-column
          align="center"
          prop="floors"
          label="层数"
          width="180"
        />
        <el-table-column
          align="center"
          prop="area"
          label="在管面积(m²)"
          width="180"
        />
        <el-table-column
          align="center"
          prop="propertyFeePrice"
          label="物业费(元/m²)"
          width="180"
        />
        <el-table-column align="center" prop="status" label="状态" width="180">
          <template v-slot="{ row }">
            <span> {{ row.status === 0 ? "空闲中" : "租赁中" }} </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template #default="{ row }">
            <el-button size="mini" type="text" style="margin-right: 20px;" @click="edit(row.id)">编辑</el-button>
            <el-button size="mini" type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end">
        <el-pagination
          :page-size="pageParams.pagesize"
          :current-page="pageParams.page"
          :total="pageParams.total"
          layout="prev, pager, next, jumper, total"
          @current-change="changePage"
          @size-change="changeCurrent"
        />
      </el-row>
    </div>
    <el-dialog :title="showTitle" :visible.sync="showDialog" @close="btnCancel">
      <el-form ref="addForm" :rules="addrules" :model="addForm" style="width: 100%; padding:0 40px;" label-width="80px">
        <el-form-item label="楼宇名称" prop="name">
          <el-input v-model="addForm.name" placeholder="请输入楼宇名称" size="mini" />
        </el-form-item>
        <el-form-item label="楼层" prop="floors">
          <el-input v-model.number="addForm.floors" placeholder="请输入楼层" size="mini" />
        </el-form-item>
        <el-form-item label="在管面积" prop="area">
          <el-input v-model.number="addForm.area" placeholder="请输入在管面积" size="mini" />
        </el-form-item>
        <el-form-item label="物业费" prop="propertyFeePrice">
          <el-input v-model.number="addForm.propertyFeePrice" placeholder="请输入物业费" size="mini" />
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="btnCancel">取消</el-button>
          <el-button size="mini" type="primary" @click="btnOK">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { delBuildingAPI, addBuildingAPI, updateBuildingAPI, getBuildingAPI, getBuildingByIdAPI } from '@/api'

export default {
  name: 'BuilDing',
  data() {
    return {
      showDialog: false,
      addForm: {
        // id: null,
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      addrules: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼层', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入在管面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入物业费', trigger: 'blur' }
        ]
      },
      pageParams: {
        name: '',
        page: 1,
        pagesize: 10,
        total: 0
      },
      loading: false,
      buildingList: []
    }
  },
  computed: {
    showTitle() {
      return this.addForm.id ? '编辑楼宇' : '添加楼宇'
    }
  },
  created() {
    this.getbuilding()
  },
  methods: {
    async getbuilding() {
      const { rows, total } = await getBuildingAPI(this.pageParams)
      this.buildingList = rows
      this.pageParams.total = total
    },
    async changePage(newPage) {
      this.loading = true
      this.pageParams.page = newPage
      await this.getbuilding()
      this.loading = false
    },
    async changeCurrent(newCurrent) {
      this.loading = true
      this.pageParams.pageSize = newCurrent
      await this.getbuilding()
      this.loading = false
    },
    async queryBuild() {
      this.loading = true
      await this.getbuilding()
      this.loading = false
    },
    btnCancel() {
      this.$refs.addForm.resetFields()
      this.showDialog = false
      this.addForm = {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      }
    },
    async btnOK() {
      this.$refs.addForm.validate()
      if (this.addForm.id) {
        await updateBuildingAPI(this.addForm)
        this.btnCancel()
        this.$message.success('编辑成功')
        this.getbuilding()
      } else {
        const res = await addBuildingAPI(this.addForm)
        console.log(res)
        this.btnCancel()
        this.$message.success('添加成功')
        this.getbuilding()
      }
    },
    async edit(id) {
      const res = await getBuildingByIdAPI(id)
      // console.log(res)
      this.showDialog = true
      this.addForm = {
        id: res.id,
        name: res.name,
        floors: res.floors,
        area: res.area,
        propertyFeePrice: res.propertyFeePrice
      }
    },
    async del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delBuildingAPI(id)
        this.$message.success('删除成功')
        this.getbuilding()
      })
    }
  }
}
</script>
<style ></style>
